//
//  ZSVistorView.swift
//  01-ZSweibo
//
//  Created by shan on 15/12/16.
//  Copyright © 2015年 LZS. All rights reserved.
//

import UIKit

/// 设置代理

protocol ZSVistorViewDelegate: NSObjectProtocol{
    
    // 协议方法--注册按钮被点击
    func registerBtnClick()
    // 协议方法--登陆按钮点击
    func loginBtnClick()
}
class ZSVistorView: UIView {


  /*   1.懒加载所有控件
     2.添加控件
     3.设置位置frame
  */
    
    /// 设置代理属性
    weak var delegate: ZSVistorViewDelegate?
    
    // 代码创建控件的时候调用
    override init(frame: CGRect) {
        super.init(frame:frame)
        // 设置父view 的背景
        self.backgroundColor = UIColor(white: 237 / 255.0 , alpha: 1)
        // 布局控件
        prepareUI()
    }
    
    // 从xib/storyboard创建控件的时候调用
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 旋转转轮动画
    func startRotationAnimation() {
       
        // 核心动画---旋转
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        // 角度
        animation.toValue = 2 * M_PI
        // 重复次数
        animation.repeatCount = MAXFLOAT
        // 一圈的时间
        animation.duration = 20
        
        // 动画完成时不移除
        animation.removedOnCompletion = false
        
        // 给图层添加核心动画
        iconImage.layer.addAnimation(animation, forKey: nil)

    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconImage.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconImage.layer.speed = 0
        
        // 设置动画偏移时间
        iconImage.layer.timeOffset = pauseTime
    }
    
    /// 恢复动画
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconImage.layer.timeOffset
        
        // 设置动画速度为1
        iconImage.layer.speed = 1
        
        // 重置偏移时间
        iconImage.layer.timeOffset = 0
        
        // 重置开始时间
        iconImage.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconImage.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconImage.layer.beginTime = timeSincePause
    }
    
    /// 除首页外其他控制器来设置访客视图
    func setupVistorView(imageName: String, message: String){
        
        // 替换转轮
        iconImage.image = UIImage(named: imageName)
        // 隐藏房子
        houseImage.hidden = true
        // 隐藏遮层
         coverView.hidden = true

        // 设置文字便签
        textLable.text = message
        // 文字多行
        textLable.numberOfLines = 0
    }
    
    // 准备UI
    private func prepareUI(){
        /// 添加子控件
        addSubview(iconImage) //转轮
        // 遮罩---- 遮住转轮就可以了
        addSubview(coverView)
        
        addSubview(loginButton)
        addSubview(registerButton)
        addSubview(textLable)
        addSubview(houseImage)
        
        
        // 设置Autoresizing为false 避免冲突
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        textLable.translatesAutoresizingMaskIntoConstraints = false
        houseImage.translatesAutoresizingMaskIntoConstraints = false
        iconImage.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        // 转轮的bound
        self.addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterX
            , relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 房子
        self.addConstraint(NSLayoutConstraint(item: houseImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: houseImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 文字便签
        self.addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        // 注册
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 宽度
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高度
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登陆按钮
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))

        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 宽度
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        /// 遮罩约束
        // 顶部
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        // 左----和父view 相等
         self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        // 右
         self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        // 底部 ---和注册按钮底部相等
         self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }
    
    // MARK: -通知代理
   @objc private func registerClick(){
        
        // 通知代理 ，有值就执行registerBtnClick
        delegate?.registerBtnClick()
        
    }
    
    @objc private  func loginClick(){
        delegate?.loginBtnClick()
    }
    
    
//    MARK: -懒加载
    // 登陆按钮
    lazy var loginButton: UIButton = {
        
        let button = UIButton()
        // 设置按钮背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        // 设置按钮文字
        button.setTitle("登陆", forState: UIControlState.Normal)
        // 设置文字颜色
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        // 设置点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        // 设置按钮背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        // 设置按钮文字
        button.setTitle("注册", forState: UIControlState.Normal)
        // 设置文字颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        // 添加点击事件
        button.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
    }()
    
    // 文字标签
    lazy var textLable: UILabel = {
        
      let label = UILabel()
        
       label.text = "关注一些人，看看这里有什么惊喜！"
      label.textColor = UIColor.darkGrayColor()
        
      return label
    }()
    
    // 房子图片
    lazy var houseImage: UIImageView = {
        
        let image = UIImageView()
        
        image.image = UIImage(named: "visitordiscover_feed_image_house")
        return image
        
    }()
    
    // 转轮
    lazy var iconImage: UIImageView = {
        let image = UIImageView()
        image.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return image
    }()
    
    // 遮罩
    lazy var coverView: UIImageView = {
       
        let coverView = UIImageView()
        coverView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return coverView
    }()
    
}
